<style>
</style>
<template>
  <list_url ref="list_url" :config="config" is_scroll="1">
    <template #action="{row,col}">
      <el-button v-for="can_action in can_action_row(row.id)" size="small" @click="click_action_row(can_action, row)">{{ can_action.label }}</el-button>
    </template>
  </list_url>
</template>
<script>
import {_config, _href, camel_str, decode_btns} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {confirm, href_handle, iframe, in_right_admin} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {S94Model} from "@/js/S94Model.js";

export default {
  props: {
    __iframe: Object,
    model: S94Model,
    this_action: Object,
  },
  data(){
    let app = this, btns = this.action_all(), is_del = this.$get('is_del');
    return {
      action_map: {},
      config: {
        api: '',
        title: `${this.model.label}${this.this_action.label}${is_del?'(已删除)':''}`,
        btns: btns,
        forms_config: {},
        list_cols: [],
        resdata: (list)=>{
          //可执行的操作
          this.can_action_list_map = {}
          list.forEach(row=>{
            let can_action_list = [];
            let ban_action = app.this_action.config.ban_action || [];
            if (typeof ban_action === "string") ban_action = ban_action.split(",");
            ban_action.push(app.this_action.action);
            let ban_type = ["restore","delete","add","del","export_list","recycle","list"];
            this.model.canActionListAdmin(row).forEach((action_row)=> {
              if (~ban_action.indexOf(action_row.action)) return;
              if (~ban_type.indexOf(action_row.type)) return;
              can_action_list.push(action_row);
            })
            this.can_action_list_map[row.id] = can_action_list;
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },

      can_action_list_map: {}, // {id:[]}
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model.name;
      return s94Mjs.param_encode(data);
    },
    can_action_row(id){
      return this.can_action_list_map[id] || [];
    },
    click_action_row(action_row, data){
      let app = this;
      let url = `/admin/${this.model.name}/${action_row.action}`;
      if (action_row.type === 'export_info'){
        window.open(_href(`${url}?id=${data.id}`), '_blank')
      }else if (action_row.form_param_ids){
        let param = {id:data.id, model_name:this.model.name, action:action_row.action};
        if (action_row.type === 'info') param['is_info'] = 1;
        iframe(`model_v2_base/edit?${s94Mjs.param_encode(param)}`);
      }else {
        confirm(`是否执行 【${action_row.label}】 操作`).then(()=>{
          let param = {id:data.id}
          switch (action_row.type){
            case "pay": param["pay_type"] = "yue"; break;
            case "refund_apply": param = Object.assign(param, {
              refund_apply: {title:"系统退款", "reason_user":"管理员发起的系统全额退款"}
            }); break;
          }
          ajax({url:url, data:param, methods:"POST"}, function(res){
            app.$refs['list_url'].list();
          })
        }).catch(()=>{})
      }
    },
    action_all(){
      let app = this;
      let res = {};
      let ban_action = this.this_action.config.ban_action || [];
      if (typeof ban_action === "string") ban_action = ban_action.split(",");
      ban_action.push(this.this_action.action);
      let limit_type = this.$get('is_del') ? ["restore","delete"] : ["add","del",'list','export_list',"export","recycle"];
      let controller = camel_str(this.model.name);
      this.model.canActionListAdmin({}).forEach((action_row)=> {
        if (~ban_action.indexOf(action_row.action)) return;
        if (!~limit_type.indexOf(action_row.type)) return;
        if (!in_right_admin(controller+'/'+action_row.action)) return;

        switch (action_row.type) {
          case "restore":case "delete":case "del":{
            res[action_row.label] = function(){ app.$refs['list_url'].ids_action('', action_row.action, false, {action_label:action_row.label}) }
          }break;
          case "add":{
            res[action_row.label] = function(){ iframe('model_v2_base/edit?'+app.param({action:action_row.action})) }
          }break;
          case "list":{
            res[action_row.label] = function(){ iframe('model_v2_base/index?'+app.param({action:action_row.action})) }
          }break;
          case "recycle":{
            res[action_row.label] = function(){ iframe('model_v2_base/index?'+app.param({action:action_row.action, is_del:1})) }
          }break;
          case "export_list":{
            res[action_row.label] = function(){
              let param = s94Mjs.param_encode(app.$refs['list_url'].$refs['forms'].form_data());
              let url = `/admin/${this.model.name}/${action_row.action}?${param}`;
              window.open(_href(url), '_blank')
            }
          }break;
        }
      })
      // 扩展总操作
      let extend_btns = this.this_action.config.extend_btns || [];
      Object.values(extend_btns).forEach((row)=>{
        switch (row.type) {
          case "iframe": {
            res[row.title] = function(){ iframe(row.href); }
          }break;
        }
      })
      // 类目列表
      let parent_model_arr = app.model.parent_model ? app.model.parent_model.split(",") : [];
      if(~parent_model_arr.indexOf("category") && in_right_admin('Category/list')){
        res[app.model.label+'类目'] = function(){ iframe('/category/index?model_name='+app.model.name) }
      }
      // 解析按钮
      return decode_btns(res);
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (!app.model.name){
      return this.__iframe ? iframe.hide('model_v2_base/index') : href_handle(-1);
    }
    let is_del = app.$get('is_del');
    ajax({url:'/admin/'+app.model.name+'/'+this.this_action.action+'?_is_init_=1'}, function(res){
      //筛选配置，默认值可以通过 字段_方式 ，提供，例如 title_like=标题
      let forms_config = {};
      res.where.forEach((v)=>{
        let key = v.field.replace('.','_')+'_'+v.method;
        if(/^list_ids?$/.test(v.type)) v['no_table'] = 1;
        v.value = app.$get(key, "");
        forms_config[key] = v;
      })
      app.config.forms_config = forms_config;
      // 列表配置
      let list_cols = res.data;
      if (!_config('this_tenant.id')){
        list_cols.push({name:'tenant_id', label:'租户', width:'65px', fixed:'right'});
      }
      let type_list = list_cols.map((v)=>{return v.type});
      if (!~type_list.indexOf("action")){
        list_cols.push({name:'action', label:'操作', width:'80px', type:'action', fixed:'right'});
      }
      app.config.list_cols = list_cols;

      //初始化操作
      setTimeout(function(){
        app.$refs.list_url.$refs.forms.set_init_value();
        app.config.api = '/admin/' + app.model.name + '/' + app.$get("action", (is_del ? 'recycle' : 'list'));
        app.$refs.list_url.list();
      })
    })

  }
}
</script>
